<?php
class Logs extends Dao
{
	private static $_instance = null;
	protected $_db = null, $_cache = null;
	
	protected function __construct()
	{
		parent::__construct();
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	public function getList(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(
				'user_id' => null,
				'log_action' => null,
				'log_time' => array(
					'from' => null,
					'to' => null
				),
			);
			$args = $this->args_merge($defaults, $args);
			$intStart = ($intPage-1)*$intLimit;						
			
			$strWhere = ''; 
			if( !empty($args['log_time']['from']) && !empty($args['log_time']['to']) ){
				$strWhere .= " AND DATE(log_time) BETWEEN '{$args['log_time']['from']}' AND '{$args['log_time']['to']}' ";
			}
			else if( empty($args['log_time']['from']) && !empty($args['log_time']['to']) ){
				echo 'vo day';
				$strWhere .= " AND DATE(log_time) <= '{$args['log_time']['to']}' ";
			}
			else if( !empty($args['log_time']['from']) && empty($args['log_time']['to']) ){
				$strWhere .= " AND DATE(log_time) >= '{$args['log_time']['from']}' ";
			}
			
			$strSql = " SELECT SQL_CALC_FOUND_ROWS *
						FROM tbl_logs
						WHERE log_action = IFNULL(:action, log_action) {$strWhere}
						ORDER BY log_time DESC
						LIMIT :intStart, :intLimit";

			$cmd = $this->_db->createCommand($strSql);
			$cmd->bindParam(":action",$args['log_action'],PDO::PARAM_STR);
			$cmd->bindParam(":intStart",$intStart,PDO::PARAM_INT);
			$cmd->bindParam(":intLimit",$intLimit,PDO::PARAM_INT);
			
			$arrResults = $cmd->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function insert(array $args)
	{		
		try{
			$defaults = array(
				'user_id' => null,
				'user_ip' => null,
				'user_agent' => null,
				'ref_id' => null,
				'log_time' => new CDbExpression('NOW()'),
				'log_action' => null,
				'log_message' => null,
				'log_data' => null				
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			$cmd->insert('tbl_logs',$args);
			return Yii::app()->db->getLastInsertId();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function delete($id){
		try{
			$cmd = $this->_db->createCommand();
			return $cmd->delete('tbl_logs', 'log_id=:id', array(':id'=>$id));
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function truncate()
	{
		try{
			return $this->_db->createCommand()->truncateTable('tbl_logs');
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
}